﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using NSsh.Common.Packets;
using NSsh.Common.Types;
using System.IO;
using NUnit.Framework.SyntaxHelpers;
using NSsh.Common.Utility;

namespace NSsh.Common.Tests.Packets
{
    [TestFixture]
    public class IgnorePacketFixture
    {
        [SetUp]
        public void Setup()
        {
            Dependency.Clear();
            Dependency.RegisterTransient<ISecureRandom, SecureRandom>("SecureRandom");
        }

        [Test]
        public void ShouldCreateFromPacketType()
        {
            Packet packet = new IgnorePacket();
            Assert.That(packet.PacketType, Is.EqualTo(PacketType.Ignore), "Incorrect packet type.");
        }

        [Test]
        public void ShouldCreateFromBinaryReader()
        {
            int length = 15;
            int paddingLength = 1;
            byte[] randomPadding = { 0 };
            string data = "data";
            MemoryStream memoryStream = new MemoryStream();

            BinaryWriter writer = new BinaryWriter(memoryStream);
            writer.WriteBE((uint)length);
            writer.Write((byte)paddingLength);

            writer.Write((byte)PacketType.Ignore);

            writer.Write(new SshByteArray(Encoding.ASCII.GetBytes(data)).ToByteArray());

            writer.Write(randomPadding);

            // Reset the stream
            memoryStream.Seek(0, SeekOrigin.Begin);

            // Create from the binary reader
            BinaryReader reader = new BinaryReader(memoryStream);
            IgnorePacket ignorePacket = new IgnorePacket(new SshPacketContext(reader));

            Assert.That(ignorePacket.Data, Is.EqualTo(Encoding.ASCII.GetBytes(data)));

            PacketFixture.ComparePacketByteData(ignorePacket, memoryStream);
            Assert.That(ignorePacket.Length, Is.GreaterThanOrEqualTo(memoryStream.Length - 4));
        }
    }
}
